﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Pur;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.Finance;

namespace ESOL.WinUI.Pur
{
    public partial class frmManageOverViewDetail : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private PurPurchaseOrderEntity mSelectedPurPurchaseOrder;
        #endregion

        #region Constructor
        public frmManageOverViewDetail()
        {
            InitializeComponent();
        }

        public frmManageOverViewDetail(MstSupplierEntity supplier)
        {
            InitializeComponent();
            if (supplier != null)
            {
                txtSupplier.Text = supplier.SupplierName;
                txtSupplier.Tag = supplier;
            }
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "PURST", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUNIT, "UNIT", true);
        }

        private string GetCondition()
        {
            string condition = " AND PO.STATUS != 'PURST01' AND PO.STATUS != 'PURST02' AND PO.STATUS != 'PURST04' ";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND PO.SUPPLIER_ID = '" + supplier.SupplierId + "' ";
            }
            if (dtpStartDate.EditValue!=null)
            {
                condition += " AND PO.PURCHASE_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpStartDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpEndDate.EditValue!=null)
            {
                condition += " AND PO.PURCHASE_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpEndDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PO.PURCHASE_ORDER_CODE";
            DataTable dt = PurPurchaseOrderBs.GetByCondition(condition, "", orderBy, rowCount,"");
            DataColumn dc = new DataColumn("IS_SELECTED", typeof(bool));
            dc.DefaultValue = false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
        }

        private void BindDetailGrid(string purchaseOrderID)
        {
            if (string.IsNullOrEmpty(purchaseOrderID))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = PurPurchaseOrderItemBs.GetDataSetByPurchaseOrderID(purchaseOrderID);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }


        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPurPurchaseOrder = new PurPurchaseOrderEntity(dr);
            BindDetailGrid(mSelectedPurPurchaseOrder.PurchaseOrderId);
        }

        protected override void Click_View()
        {
            if (mSelectedPurPurchaseOrder == null)
            {
                MessageBox.Show("请选择一个采购单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseOrder frm = new frmMaintainPurchaseOrder();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPurchaseOrder = mSelectedPurPurchaseOrder;
            frm.ShowDialog();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        DataRow selectDr;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            selectDr = gridView1.GetDataRow(e.FocusedRowHandle);

        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            if ((bool)selectDr["IS_SELECTED"])
            {
                selectDr["IS_SELECTED"] = false;
            }
            else
            {
                selectDr["IS_SELECTED"] = true;
            }
        }

        #endregion

        #region Button Event
        private void btnPurchaseApplication_Click(object sender, EventArgs e)
        {
            MstSupplierEntity supplier = null;
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                supplier = (MstSupplierEntity)txtSupplier.Tag;
            }
            frmMaintainPurchaseApplication frm = new frmMaintainPurchaseApplication();
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnInvoiceApplication_Click(object sender, EventArgs e)
        {
            MstSupplierEntity supplier = null;
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                supplier = (MstSupplierEntity)txtSupplier.Tag;
            }

            frmMaintainAPInvoice frm = new frmMaintainAPInvoice();
            FinBillingEntity entity = new FinBillingEntity();
            entity.SupplierId = supplier.SupplierId;
            //entity.ApplicationId = Guid.NewGuid().ToString();
            entity.BillingId = Guid.NewGuid().ToString();
            frm.ThisActionType = ActionType.New;
            frm.SelectedBilling = entity;

            frm.ShowDialog();
        }

        private void btnReturnOrder_Click(object sender, EventArgs e)
        {

        }

        private void btnPurchaseQuoteOrder_Click(object sender, EventArgs e)
        {
            MstSupplierEntity supplier = null;
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                supplier = (MstSupplierEntity)txtSupplier.Tag;
            }
            frmMaintainPurchaseQuoteOrder frm = new frmMaintainPurchaseQuoteOrder(null, supplier);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnPurchaseOrder_Click(object sender, EventArgs e)
        {
            MstSupplierEntity supplier = null;
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                supplier = (MstSupplierEntity)txtSupplier.Tag;
            }
            frmMaintainPurchaseOrder frm = new frmMaintainPurchaseOrder(null, supplier);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnPurchaseArrivedOrder_Click(object sender, EventArgs e)
        {
            List<PurPurchaseOrderEntity> list = new List<PurPurchaseOrderEntity>();
            DataTable dt = gvMain.DataSource as DataTable;
            for (int i = 0; i <  gridView1.DataRowCount; i++)
            {
                if (dt.Rows[i]["IS_SELECTED"] == null || Convert.ToBoolean(dt.Rows[i]["IS_SELECTED"].ToString()) == false)
                {
                    continue;
                }
                PurPurchaseOrderEntity entity = new PurPurchaseOrderEntity(dt.Rows[i]);
                list.Add(entity);
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请至少选择一个采购单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseArrivedOrder frm = new frmMaintainPurchaseArrivedOrder(list);
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        #endregion

    }
}
